package cn.jnetty.ioc.scan;
import cn.jnetty.api.annotation.*;
import cn.jnetty.ioc.definition.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 猪哥亮
 * @email 1030681978@qq.com
 * @date 2020/4/9 12:18
 * 描述：
 */
public class AnnotationHandler {
    private static final  Logger log = LoggerFactory.getLogger(AnnotationHandler.class);
    private static class AnnotationBean{
        private Annotation annotation;
        private Class<?> clazz;
        public AnnotationBean(Annotation annotation, Class<?> clazz) {
            this.annotation = annotation;
            this.clazz = clazz;
        }
    }
    private static Map<Class<? extends Annotation>,List<AnnotationBean>> map = new HashMap<>();
    /**
     * 分析Class
     *
     * @param klass Class
     */
    public static void autowired(Class<?> klass){
        // 接口、内部类、枚举、注解和匿名类 直接忽略
        if (klass.isInterface() || klass.isMemberClass() || klass.isEnum() || klass.isAnnotation() || klass.isAnonymousClass()) {
            return;
        }
        // 抽象类和非Public的也忽略
        int modify = klass.getModifiers();
        if (Modifier.isAbstract(modify) || (!Modifier.isPublic(modify))) {
            return;
        }
        for (Annotation annotation : klass.getAnnotations()) {
            Class<? extends Annotation> annotationType = annotation.annotationType();
            log.debug("扫描到的jnetty-ioc注解组件-"+ klass + " ================= " + annotationType.getName());
            List<AnnotationBean> list = map.getOrDefault(annotationType,new ArrayList<>());
            list.add(new AnnotationBean(annotation,klass));
            map.put(annotationType,list);
        }
    }

    /**
     * 注入ioc容器
     */
    public static void autowired(){
        //1 扫描@JComponent @JController注解
        List<AnnotationBean> components = map.getOrDefault(JComponent.class,new ArrayList<>(0));
        components.stream().forEach(c -> analytical(c.clazz,JComponent.class.cast(c.annotation)));
        List<AnnotationBean> controllers = map.getOrDefault(JController.class,new ArrayList<>(0));
        controllers.stream().forEach(c -> analytical(c.clazz,JController.class.cast(c.annotation)));
        //2 扫描@JConfiguration注解
        List<AnnotationBean> configurations = map.getOrDefault(JConfiguration.class,new ArrayList<>(0));
        configurations.stream().forEach(c -> analytical(c.clazz,JConfiguration.class.cast(c.annotation)));
        //3 自动装配属性
        JDefaultBeanDefinition.autowired();
        //4 装配路由
        List<AnnotationBean> rRequestMapping = map.getOrDefault(JRequestMapping.class,new ArrayList<>(0));
        rRequestMapping.stream().forEach(c -> analytical(c.clazz,JRequestMapping.class.cast(c.annotation)));
        //5 开启socket
        List<AnnotationBean> enableSocket = map.getOrDefault(JEnableSocket.class,new ArrayList<>(0));
        enableSocket.stream().forEach(c -> analytical(c.clazz,JEnableSocket.class.cast(c.annotation)));
        //5 开启excel配置文件
        List<AnnotationBean> enableExcel = map.getOrDefault(JEnableExcel.class,new ArrayList<>(0));
        enableExcel.stream().forEach(c ->  analytical(c.clazz,JEnableExcel.class.cast(c.annotation)));
        //清空缓存
        map.clear();
        map = null;
    }
    private static void analytical(Class<?> clazz,JConfiguration component) {
        new JConfigurationBeanDefinition(component,clazz);
    }
    private static void analytical(Class<?> clazz,JRequestMapping component) {
        if(clazz.isInterface()){
            return;
        }
        new JRequestMappingBeanDefinition(component,clazz);
    }
    private static void analytical(Class clazz,JEnableSocket component) {
        new JSocketBeanDefinition(component,clazz);
    }

    private static void analytical(Class<?> clazz,JComponent component) {
        new JComponentBeanDefinition(component,clazz);
    }
    private static void analytical(Class<?> clazz,JController component) {
        new JControllerBeanDefinition(component,clazz);
    }
    private static void analytical(Class<?> clazz,JEnableExcel component) {
        new JEnableExcelBeanDefinition(component,clazz);
    }
}
